const _function = require("../../utils/functionData.js")
const { formatTime } = require("../../utils/util")
Page({
  data: {
    reservations: [
    //   {
    //   id: 1,
    //   avatar: '/images/avatar.png',
    //   studentName: '张三',
    //   studentId: '2023001',
    //   reservationTime: '2023-12-20 14:30',
    //   status: 0 // 0-待处理 1-已同意 2-已拒绝
    // }
  ],
    curStudentId: '',
    _UrlGet_: wx.getStorageSync('img_url')
  },
  onLoad() {
    this.getBookStudentList()
  },
  onShow() {
    this.getBookStudentList()
    if (this.getTabBar) {
      this.getTabBar().setData({
        selected: 2 // 根据当前页面设置选中状态
      });
    };
  },
  // 获取学生预约列表
  getBookStudentList(e) {
    console.log('选择时间', getApp().globalData.userInfo);
    const param = {
      teacherId: getApp().globalData.userInfo.userId
    }
    _function.getBookStudentList(param, this.getBookStudentListBack, this)
  },
  getBookStudentListBack(res) {
    let dataAll = res.data
    let that = this
    if (dataAll.code == 200) {
      console.log('视频列表', dataAll);
      let arr = []
      dataAll.rows.forEach((item, index) => {
        if (item.isAgree == "" || item.isAgree == null) {
          arr.push(item)
        }
      })
      console.log('arr',arr)
      that.setData({
        reservations: arr
      })
    }
  },
  confirmAppointment(e) {
    const id = e.currentTarget.dataset.id
    this.setData({
      curStudentId: e.currentTarget.dataset.item.studentId
    })
    // 处理确认逻辑
    const param = {
      id: String(id),
      isAgree: 'Y'
    }
    _function.putBooking(param, this.postBookingBack, this)
  },

  rejectAppointment(e) {
    const id = e.currentTarget.dataset.id
    console.log(321, e);
    // 处理确认逻辑
    const param = {
      id,
      isAgree: 'N'
    }
    _function.putBooking(param, this.postBookingBack, this)
  },
  postBookingBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {
      if (this.data.curStudentId) {
        this.postMesList(this.data.curStudentId)
      }
      this.getBookStudentList()
      wx.showToast({
        title: dataAll.msg,
        icon: 'none',
        duration: 1500, 
        mask: true, 
        success: () => {
        }
      });
    }
  },
  // 添加消息列表
  postMesList(id) {
    // const id = e.currentTarget.dataset.id
    const now = new Date(); // 获取当前时间
    const formattedTime = formatTime(now); // 使用 formatTime 函数格式化时间
    this.setData({
      formattedTime // 更新页面数据
    });
    // 处理确认逻辑
    const param = {
      teacherId: getApp().globalData.userInfo.userId,
      studentId: id,
      startTime: formattedTime
    }
    console.log('参数', param);
    _function.postMesList(param, this.postMesListBack, this)
  },
  postMesListBack(res) {
    let dataAll = res.data
    if (dataAll.code == 200) {
      console.log('视频列表', dataAll);
      
      wx.showToast({
        title: dataAll.msg,
        icon: 'none',
        duration: 1500, 
        mask: true, 
      });
    }
  },
})